from django.core.mail import send_mail
from meiduo_mall.settings import EMAIL_FROM
from celery_tasks.main import app


@app.task(bind=True, default_retry_delay=5)
def send_activate_mail(self, email, verify_url):
    """
    发送激活邮件
    :param email: 激活邮件的收信地址
    """
    # 组织激活邮件的内容
    subject = '美多商城激活邮件'
    message = '内容'
    from_email = EMAIL_FROM  # 发件人
    recipient_list = [email]  # 收件人列表
    subject = "美多商城邮箱验证"
    html_message = '<p>尊敬的用户您好！</p>' \
                   '<p>感谢您使用美多商城。</p>' \
                   '<p>您的邮箱为：%s 。请点击此链接激活您的邮箱：</p>' \
                   '<p><a href="%s">%s<a></p>' % (email, verify_url, verify_url)
    # 发送邮件
    try:
        send_mail(subject=subject, message=message,
                html_message=html_message,
                from_email=from_email,
                recipient_list=recipient_list)
    except Exception as e:
        # 失败后重试，最多3次
        self.retry(exc=e, max_retries=3)
